/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package clases.informeTecnico;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import managers.managerModificarExpediente;
import org.primefaces.event.FileUploadEvent;

/**
 *
 * @author olvarey
 */
public class Entorno implements Serializable {

    private String perfilCuadra;
    private String lineaConstruccion;
    private String perfilFrenteCuadra;
    private double anchoAcera;
    private double anchoRodaje;
    private double anchoArriate;
    private String valorizacion; //variable del punto 7
    private static final int BUFFER_SIZE = 6124;

    public Entorno() {
        perfilCuadra = "";
        lineaConstruccion = "";
        perfilFrenteCuadra = "";
    }

    public double getAnchoAcera() {
        return anchoAcera;
    }

    public void setAnchoAcera(double anchoAcera) {
        this.anchoAcera = anchoAcera;
    }

    public double getAnchoArriate() {
        return anchoArriate;
    }

    public void setAnchoArriate(double anchoArriate) {
        this.anchoArriate = anchoArriate;
    }

    public double getAnchoRodaje() {
        return anchoRodaje;
    }

    public void setAnchoRodaje(double anchoRodaje) {
        this.anchoRodaje = anchoRodaje;
    }

    public String getLineaConstruccion() {
        return lineaConstruccion;
    }

    public void setLineaConstruccion(String lineaConstruccion) {
        this.lineaConstruccion = lineaConstruccion;
    }

    public String getPerfilCuadra() {
        return perfilCuadra;
    }

    public void setPerfilCuadra(String perfilCuadra) {
        this.perfilCuadra = perfilCuadra;
    }

    public String getPerfilFrenteCuadra() {
        return perfilFrenteCuadra;
    }

    public void setPerfilFrenteCuadra(String perfilFrenteCuadra) {
        this.perfilFrenteCuadra = perfilFrenteCuadra;
    }

    public String getValorizacion() {
        return valorizacion;
    }

    public void setValorizacion(String valorizacion) {
        this.valorizacion = valorizacion;
    }

    public void uploadPictureCuadra(FileUploadEvent e) {
        ExternalContext extContext = FacesContext.getCurrentInstance().getExternalContext();
        String nombreArchivoCompleto = e.getFile().getFileName();
        String[] splitFullName = nombreArchivoCompleto.split("\\.");
        String rutaPadre = extContext.getRealPath("//imagenesInmuebles");
        String fileNumber = managerModificarExpediente.numeroExpediente;
        File carpeta = new File(rutaPadre + "//" + fileNumber);
        if (!carpeta.exists()) {
            carpeta.mkdir();
        }

        File result = new File(extContext.getRealPath("//imagenesInmuebles") + "//" + fileNumber + "//" + fileNumber +"-CUADRA"+ "." + splitFullName[1]);
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(result);

            byte[] buffer = new byte[BUFFER_SIZE];

            int bulk;
            InputStream inputStream = e.getFile().getInputstream();
            while (true) {
                bulk = inputStream.read(buffer);
                if (bulk < 0) {
                    break;
                }
                fileOutputStream.write(buffer, 0, bulk);
                fileOutputStream.flush();
            }

            fileOutputStream.close();
            inputStream.close();
            perfilCuadra = "..//imagenesInmuebles//" + fileNumber + "//" + fileNumber +"-CUADRA"+ "." + splitFullName[1];
            FacesMessage msg = new FacesMessage("SUBIDA DE ARCHIVO", "El archivo ha sido subido con exito");
            FacesContext.getCurrentInstance().addMessage(null, msg);

        } catch (IOException error) {
            error.printStackTrace();
            FacesMessage mensajeError = new FacesMessage("The files were not uploaded!");
            FacesContext.getCurrentInstance().addMessage(null, mensajeError);
        }
    }

    public void uploadPictureFrente(FileUploadEvent e) {
        ExternalContext extContext = FacesContext.getCurrentInstance().getExternalContext();
        String nombreArchivoCompleto = e.getFile().getFileName();
        String[] splitFullName = nombreArchivoCompleto.split("\\.");
        String rutaPadre = extContext.getRealPath("//imagenesInmuebles");
        String fileNumber = managerModificarExpediente.numeroExpediente;
        File carpeta = new File(rutaPadre + "//" + fileNumber);
        if (!carpeta.exists()) {
            carpeta.mkdir();
        }

        File result = new File(extContext.getRealPath("//imagenesInmuebles") + "//" + fileNumber + "//" + fileNumber + "-FRENTE"+"." + splitFullName[1]);
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(result);

            byte[] buffer = new byte[BUFFER_SIZE];

            int bulk;
            InputStream inputStream = e.getFile().getInputstream();
            while (true) {
                bulk = inputStream.read(buffer);
                if (bulk < 0) {
                    break;
                }
                fileOutputStream.write(buffer, 0, bulk);
                fileOutputStream.flush();
            }
            fileOutputStream.close();
            inputStream.close();
            //System.out.println("Saca el usuario: "+extContext.getUserPrincipal().getName());
            perfilFrenteCuadra = "..//imagenesInmuebles//" + fileNumber + "//" + fileNumber + "-FRENTE"+"." + splitFullName[1];
            FacesMessage msg = new FacesMessage("SUBIDA DE ARCHIVO", "El archivo ha sido subido con exito");
            FacesContext.getCurrentInstance().addMessage(null, msg);

        } catch (IOException error) {
            error.printStackTrace();
            FacesMessage mensajeError = new FacesMessage("The files were not uploaded!");
            FacesContext.getCurrentInstance().addMessage(null, mensajeError);
        }
    }

    public void uploadPictureLinea(FileUploadEvent e) {
        ExternalContext extContext = FacesContext.getCurrentInstance().getExternalContext();
        String nombreArchivoCompleto = e.getFile().getFileName();
        String[] splitFullName = nombreArchivoCompleto.split("\\.");
        String rutaPadre = extContext.getRealPath("//imagenesInmuebles");
        String fileNumber = managerModificarExpediente.numeroExpediente;
        File carpeta = new File(rutaPadre + "//" + fileNumber);
        if (!carpeta.exists()) {
            carpeta.mkdir();
        }

        File result = new File(extContext.getRealPath("//imagenesInmuebles") + "//" + fileNumber + "//" + fileNumber +"-LINEA"+ "." + splitFullName[1]);
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(result);

            byte[] buffer = new byte[BUFFER_SIZE];

            int bulk;
            InputStream inputStream = e.getFile().getInputstream();
            while (true) {
                bulk = inputStream.read(buffer);
                if (bulk < 0) {
                    break;
                }
                fileOutputStream.write(buffer, 0, bulk);
                fileOutputStream.flush();
            }
            fileOutputStream.close();
            inputStream.close();
            lineaConstruccion = "..//imagenesInmuebles//" + fileNumber + "//" + fileNumber +"-LINEA"+ "." + splitFullName[1];
            FacesMessage msg = new FacesMessage("SUBIDA DE ARCHIVO", "El archivo ha sido subido con exito");
            FacesContext.getCurrentInstance().addMessage(null, msg);

        } catch (IOException error) {
            error.printStackTrace();
            FacesMessage mensajeError = new FacesMessage("The files were not uploaded!");
            FacesContext.getCurrentInstance().addMessage(null, mensajeError);
        }
    }
}
